<?php

namespace app\school\controller;
use app\common\controller\BasicAdmin;
use app\common\model\PortraitModel;
use app\common\model\PortraitCateModel;
use app\common\traits\TableSelect;
use app\common\traits\SelectPage;
use app\common\traits\SelectN;
use think\Model;
use think\db;

class Portrait extends BasicAdmin
{
    use SelectPage;
	use TableSelect;
	use SelectN;
    /**
     * @var Model
     */
    protected $model = 'PortraitModel';
    protected $title = '写真管理';
    public function index(){
        $this->_query($this->model)
            ->like('name')
            ->where(function ($db){
                if($this->request->has('portrait_cate_id','get') && !empty($this->request->get('portrait_cate_id'))){
                    $portrait_cate_id = $this->request->get('portrait_cate_id');
                    $db->where('portrait_cate_id',$portrait_cate_id);
                }
            })
            ->with(['portraitcate'])
            ->page();
    }

    public function _page_filter(&$data){
        $this->portrait_cate = PortraitCateModel::cateTree();
    }

    public function add()
    {
        return $this->_form($this->model, 'form');
    }

    public function _add_form_filter(&$data)
    {
        if ($this->request->isPost()) {
            $this->alterInfo($data);
        }
    }


    public function edit(){
        return $this->_form($this->model,'form');
    }

    public function _edit_form_filter(&$data)
    {
        if ($this->request->isPost()) {
            $this->alterInfo($data);
        }
    }

    protected function alterInfo(&$data){
        if(!isset($data['portrait_cate_id']) || empty($data['portrait_cate_id'])){
            $this->error('分类不能留空！');
        }
    }

    public function del()
    {
        return $this->_delete($this->model);
    }

    public function upshowindex(){
        $this->_save($this->model, [
            'show_index' => $this->request->request('show_index'),
        ],'id', ['id' => $this->request->request('id')]);
    }

    public function upstatus(){
        $this->_save($this->model, [
            'status' => $this->request->request('status'),
        ],'id', ['id' => $this->request->request('id')]);
    }

    public function uphot(){
        $this->_save($this->model, [
            'is_hot' => $this->request->request('is_hot'),
        ],'id', ['id' => $this->request->request('id')]);
    }

}